'use strict';

// const Controller = require('egg').Controller; 
const BaseController = require('../core/base_controller');

class SystemController extends BaseController {
    async systemParamsSet() {
        const {
            ctx
        } = this;
        let params = ctx.request.body;
        // console.log(params)
        if(Object.keys(params).length<1 ){
            this.fail("参数错误");
            return
        }
        params.id=1;
        try {
            const status = await ctx.model.System.upsert(
                // {
                // id: 1,
                params
                // wxQr: params.wxQr,
                // wxQr2: params.wxQr2,
                // wxClass:params.wxClass,
                // alpQr: params.alpQr,
                // price: params.price,
                // yieldRate: params.yieldRate,
                // yestodayWithdrawalCount: params.yestodayWithdrawalCount,
                // hostUrl: params.hostUrl,
                // appUrl:params.appUrl,
                // yestodayWithdrawalCount:params.yestodayWithdrawalCount,
                // kf:params.kf,

            // }
            )
            this.success({}, "成功");
        } catch (e) {
            this.fail("失败");
        }
    }
    //APP获取首页参数
    async getHomeDatd() {
        const {
            ctx
        } = this;
        // const params = ctx.request.body;
        console.log(ctx.header.tokenid)
        let data = await ctx.model.System.findById(1,
            //    { attributes: ["id",'yieldRate','yestodayWithdrawalCount',]}
        )
        data.dataValues.maxNum = ctx.SHOP_MACHINE_AMOUNT;
        const count = await ctx.model.User.findById(ctx.header.tokenid,{
            attributes:["recommendedAmount"]
        })
        // console.log(count.recommendedAmount,"11111")
        let newD=  JSON.parse(JSON.stringify(data));
        const list =await ctx.model.AD.findAll({ attributes: {
            exclude:["updatedAt"]
            }})
        newD.list=list;
        newD.recommendedAmount=count.recommendedAmount;         
        const isNotification = await ctx.model.Notification.findAll();
            newD.haveNoti= isNotification.length;//用于提示APP有公告消息
         this.success({
            data: newD,
            date: new Date(),
        }, "成功00");
    }
 //web获取系统参数
 async getSysDatd() {
    const {
        ctx
    } = this;
    // const params = ctx.request.body;
     
    let data = await ctx.model.System.findById(1,
        //    { attributes: ["id",'yieldRate','yestodayWithdrawalCount',]}
    )
    data.dataValues.maxNum = ctx.SHOP_MACHINE_AMOUNT;
    
    let newD=  JSON.parse(JSON.stringify(data));
        this.success({
        data: newD,
        // date: new Date(),
    }, "成功00");
}

    //app获取收款二维码 目前默认 wx
    //根据个数
    async getQR() {
        const {
            ctx
        } = this;
        const params = ctx.request.body;
        console.log("参数",params)
        let data = await ctx.model.System.findById(1,
               { attributes: ["wxQr","wxQr2","wxClass"]}
        ) 
         
         const qr= data.wxClass==1?data.wxQr:data.wxQr2;
        this.success({
            data: {
                 payQR:qr,
                canPayType:2,//目前能用的类型是 1  支付宝  2微信   不传则所有
            },
        }, "成功");
    }
}

module.exports = SystemController;